package com.maoxx.stuchoosemssbv_service.service;

import com.maoxx.stuchoosemssbv_service.entity.Course;
import com.maoxx.stuchoosemssbv_service.mapper.CourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @className: CourseService
 * @author: Maoxx
 * @date: 2024/11/4 13:20
 * @Version: 1.0
 * @description:
 */


@Service
public class CourseService {

    @Autowired
    private CourseMapper courseMapper;

    public List<Course> findBySearch(Integer cid) {
        return courseMapper.findBySearch(cid, null, 0, null, null);
    }

    public boolean deleteById(Integer cid) {
        return courseMapper.deleteById(cid);
    }

    public boolean updateCourse(Course course) {
        return courseMapper.updateById(course);
    }

    public List<Course> findBySearch(Map<String, String> map) {
        Integer cid = null;
        Integer lowBound = null;
        Integer highBound = null;
        Integer fuzzy = 0;
        String cname = null;

        if (map.containsKey("cid")) {
            try {
                cid = Integer.parseInt(map.get("cid"));
            } catch (Exception e) {
            }
        }
        if (map.containsKey("lowBound")) {
            try {
                lowBound = Integer.parseInt(map.get("lowBound"));
            } catch (Exception e) {
            }
        }
        if (map.containsKey("highBound")) {
            try {
                highBound = Integer.valueOf(map.get("highBound"));
            } catch (Exception e) {
            }
        }
        if (map.containsKey("cname")) {
            cname = map.get("cname");
        }
        if (map.containsKey("fuzzy")) {
            fuzzy = (map.get("fuzzy").equals("true")) ? 1 : 0;
        }
        System.out.println("查询课程 map：" + map);
        System.out.println(cid + " " + cname + " " + fuzzy + " " + lowBound + " " + highBound);
        return courseMapper.findBySearch(cid, cname, fuzzy, lowBound, highBound);
    }

    public List<Course> findAll() {
        return courseMapper.findBySearch(null, null, 0, null, null);
    }

    public List<Course> findPage(Integer page, Integer size) {
        List<Course> courseList = findAll();
        List<Course> list = new ArrayList<>();

        int start = size * page;
        int end = size * (page + 1);
        int sz = courseList.size();

        for (int i = start; i < end && i < sz; i++) {
            list.add(courseList.get(i));
        }

        return list;
    }

    public boolean save(Course course) {
        return courseMapper.save(course);
    }
}
